package com.mygdx.game;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.math.Vector2;

public class MyGdxGameBackup extends Game {
	
	static final int SPEED = 500;
	static final int ACCELERATION = 50;
	
	ShapeRenderer shapeRenderer;
	SpriteBatch batch;
	OrthographicCamera camera;
	
	Texture background;
	
	Animation planeAnim;
	
	Vector2 planePosition = new Vector2();
	Vector2 planeVelocity = new Vector2();
//	int velocityScale = 1;
	int decelaration = 0;
	GridPoint2 touchPoint;
	
	float planeStateTime = 0;
	int planeXOffset;
	int planeYOffset;
	
	float lastDelta;
	
	BitmapFont font;
	String score;
	
	@Override
	public void create () {
		
		shapeRenderer = new ShapeRenderer();
		batch = new SpriteBatch();
		camera = new OrthographicCamera();
		camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		
		Texture frame1 = new Texture("spaceship-icon1.png");
		frame1.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		Texture frame2 = new Texture("spaceship-icon2.png");
		Texture frame3 = new Texture("spaceship-icon3.png");
		
		planeAnim = new Animation(
				0.05f, 
				new TextureRegion(frame1), 
				new TextureRegion(frame2),
				new TextureRegion(frame3), 
				new TextureRegion(frame2));
		planeAnim.setPlayMode(PlayMode.LOOP);
		
		planeYOffset = frame1.getHeight() / 2;
		planeXOffset = frame1.getWidth() / 2;
		
		font = new BitmapFont(Gdx.files.internal("arial.fnt"));

		resetWorld();
	}
	
	private void resetWorld() {
		planePosition.set(100, 100);
		planeVelocity.set(0, 0);
		camera.position.x = Gdx.graphics.getWidth()/2;
	}

	@Override
	public void render () {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		updateWorld();
		drawWorld();	
	}
	
	float totalDelta = 0;
	float xTargetSpeed = 0;
	float yTargetSpeed = 0;
	float xAcceleration = 0;
	float yAcceleration = 0;
	
	private void updateWorld()
	{
		float deltaTime = Gdx.graphics.getDeltaTime();
		planeStateTime += deltaTime;
		
		if(Gdx.input.justTouched()) 
		{
			decelaration = 21;
			
			touchPoint = new GridPoint2(Gdx.input.getX(), Gdx.input.getY());
//			touchPoint.x -= planeXOffset;
//			touchPoint.y += planeYOffset;
			
			score = "Plane("+planePosition.x+","+planePosition.y+"), Touch("+touchPoint.x+","+touchPoint.y+")";
			Gdx.app.log("Plane", score);
			
			float deltaX = touchPoint.x - planePosition.x;
			float deltaY = (int)(Gdx.graphics.getHeight()-touchPoint.y) - planePosition.y;
			totalDelta = Math.abs(deltaX)+Math.abs(deltaY);
			lastDelta = totalDelta;
			
			Gdx.app.log("Plane", "DeltaX:"+deltaX +" - DeltaY:" + deltaY);
			float speedX = deltaX/totalDelta;
			float speedY = deltaY/totalDelta;
			
//			planeVelocity = new Vector2((int)(SPEED*speedX), (int)(SPEED*speedY));
//			planeVelocity = new Vector2(0,0);
			
			xTargetSpeed = SPEED*speedX;
			yTargetSpeed = SPEED*speedY;
			
			xAcceleration = ACCELERATION*speedX;
			yAcceleration = ACCELERATION*speedY;
			
			if (planeVelocity.x != xTargetSpeed) {
				if (planeVelocity.x < xTargetSpeed) {
					if ((xTargetSpeed - planeVelocity.x) <= xAcceleration) {
						planeVelocity.x = xTargetSpeed;
					} else
						planeVelocity.x += xAcceleration;
				} else {
					if ((xTargetSpeed - planeVelocity.x) >= xAcceleration)
						planeVelocity.x = xTargetSpeed;
					else
						planeVelocity.x += xAcceleration;
				}
			}
			
			if (planeVelocity.y != yTargetSpeed) {
				if (planeVelocity.y < yTargetSpeed) {
					if ((yTargetSpeed - planeVelocity.y) <= yAcceleration)
						planeVelocity.y = yTargetSpeed;
					else
						planeVelocity.y += yAcceleration;
				} else {
					if ((yTargetSpeed - planeVelocity.y) >= yAcceleration)
						planeVelocity.y = yTargetSpeed;
					else
						planeVelocity.y += yAcceleration;
				}
			}
			
//			velocityScale = 0;
			
//			if (planeVelocity != null) {
//				Gdx.app.log("Plane", "Velocity("+planeVelocity.x+","+planeVelocity.y+")");
//			}
		}
		
		else if (touchPoint != null && planeVelocity != null && !planeVelocity.isZero())
		{
			float deltaX = touchPoint.x - planePosition.x;
			float deltaY = (int)(Gdx.graphics.getHeight()-touchPoint.y) - planePosition.y;
			totalDelta = Math.abs(deltaX)+Math.abs(deltaY);
			
			float speedX = deltaX/totalDelta;
			float speedY = deltaY/totalDelta;
			
			xTargetSpeed = SPEED*speedX;
			yTargetSpeed = SPEED*speedY;
			
			xAcceleration = ACCELERATION*speedX;
			yAcceleration = ACCELERATION*speedY;
			
			if (planeVelocity.x != xTargetSpeed) {
				if (planeVelocity.x < xTargetSpeed) {
					if ((xTargetSpeed - planeVelocity.x) <= xAcceleration) {
						planeVelocity.x = xTargetSpeed;
						Gdx.app.log("Plane", "Max X Speed");
					} else
						planeVelocity.x += xAcceleration;
				} else {
					if ((xTargetSpeed - planeVelocity.x) >= xAcceleration)
						planeVelocity.x = xTargetSpeed;
					else
						planeVelocity.x += xAcceleration;
				}
			}
			
			if (planeVelocity.y != yTargetSpeed) {
				if (planeVelocity.y < yTargetSpeed) {
					if ((yTargetSpeed - planeVelocity.y) <= yAcceleration) {
						planeVelocity.y = yTargetSpeed;
						Gdx.app.log("Plane", "Max Y Speed");
					} else
						planeVelocity.y += yAcceleration;
				} else {
					if ((yTargetSpeed - planeVelocity.y) >= yAcceleration)
						planeVelocity.y = yTargetSpeed;
					else
						planeVelocity.y += yAcceleration;
				}
			}
			
//			if (totalDelta > lastDelta) {
//				planeVelocity.x = 0;
//				planeVelocity.y = 0;
//			} else {
//				lastDelta = totalDelta;
//			}
		}
		
		if (!planeVelocity.isZero()) {
			
			Gdx.app.log("Plane", "("+planeVelocity.x+","+planeVelocity.y+")");
			
			float distancePerTick = totalDelta - lastDelta;
//			float distancePerTick = planeVelocity.len();
			
//			Gdx.app.log("Plane", "dpt:"+distancePerTick + " - td:" + totalDelta + " - ld:" + lastDelta);
			
//			Gdx.app.log("Plane", "deceleration:"+decelaration);
			
			if (decelaration <= 20) {
				if (decelaration > 0) {
					Vector2 temp = planeVelocity.cpy();
					temp.scl((float)decelaration/(float)20);
//					Gdx.app.log("Plane", "("+planeVelocity.x+","+temp.x+")-("+planeVelocity.y+","+temp.y+")");
					planePosition.mulAdd(temp, deltaTime);
					decelaration--;
				} else {
					planeVelocity = new Vector2(0,0);
				}
			} else if (Math.abs(distancePerTick*10) > totalDelta) {
					decelaration--;
					planePosition.mulAdd(planeVelocity, deltaTime);
	//				Gdx.app.log("Plane", "deceleration:"+decelaration);
	//				if (decelaration > 0) {
	//					Vector2 temp = planeVelocity.cpy();
	//					temp.scl((float)decelaration/(float)20);
	////					Gdx.app.log("Plane", "("+planeVelocity.x+","+temp.x+")-("+planeVelocity.y+","+temp.y+")");
	//					planePosition.mulAdd(temp, deltaTime);
	//					decelaration--;
	//				} else {
	//					planeVelocity = new Vector2(0,0);
	//				}
			}
//			if (velocityScale < 20)
//			{
//				velocityScale++;
//				Gdx.app.log("Plane", "velocity scale: "+ (float)velocityScale/(float)20);
//				Vector2 temp = planeVelocity.cpy();
//				temp.scl((float)velocityScale/(float)20);
//				Gdx.app.log("Plane", "("+planeVelocity.x+","+temp.x+")-("+planeVelocity.y+","+temp.y+")");
//				planePosition.mulAdd(temp, deltaTime);
//			}
			else
			{
				planePosition.mulAdd(planeVelocity, deltaTime);
			}
		}
		
		lastDelta = totalDelta;
	}
	
	private void drawWorld()
	{
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		
		batch.draw(planeAnim.getKeyFrame(planeStateTime), planePosition.x, planePosition.y);
		
		batch.end();
		
		if (touchPoint != null) {
			ShapeRenderer renderer = new ShapeRenderer();
			renderer.setProjectionMatrix(camera.combined);
		    renderer.begin(ShapeType.Filled);
		    renderer.setColor(1,0,0,1);
		    renderer.circle(touchPoint.x, Gdx.graphics.getHeight()-touchPoint.y, 10);
		    renderer.identity();
		    renderer.end();
		}
	}
	
	@Override
	public void resize(int width, int height) {
	}
	
	@Override
	public void pause() {
	}
	
	@Override
	public void resume() {
	}
	
	@Override
	public void dispose() {
	}
}
